import {Component, OnInit} from '@angular/core';
import * as echars from 'echarts';
import {HttpClient} from '@angular/common/http';
import {Router} from '@angular/router';

@Component({
  selector: 'app-admin-home-index',
  templateUrl: './admin-home-index.component.html',
  styleUrls: ['./admin-home-index.component.css']
})
export class AdminHomeIndexComponent implements OnInit {

  books: any;
  users: any;
  loading1 = false;
  loading2 = false;

  getBookData(): void {
    const url = '/api/adminBook/getCurrentBookData';
    this.http.get(url).subscribe((resp: any) => {
      const code = resp.code;
      if (code === 200) {
        this.books = resp.data;
        const myechart = echars.init(document.getElementById('book_chart'));
        const option = {
          title: {
            text: '最近七天图书录入情况'
          },
          tooltip: {},
          legend: {
            data: ['数量']
          },
          xAxis: {
            data: this.books.time
          },
          yAxis: {},
          series: [{
            name: '数量',
            type: 'line',
            data: this.books.num
          }]
        };
        myechart.setOption(option);
        this.loading1 = true;
      }
    }, (error) => {
      if (error.status === 403 || error.status === 501) {
        this.router.navigateByUrl('/permision-error');
      }
    });
  };

  getUserData(): void {
    const url = '/api/adminUser/getCurrentUser';
    this.http.get(url).subscribe((resp: any) => {
      const code = resp.code;
      if (code === 200) {
        this.users = resp.data;
        const myechart = echars.init(document.getElementById('user_chart'));
        const option = {
          title: {
            text: '最近七天用户注册情况'
          },
          tooltip: {},
          legend: {
            data: ['数量']
          },
          xAxis: {
            data: this.users.time
          },
          yAxis: {},
          series: [{
            name: '数量',
            type: 'line',
            data: this.users.num
          }]
        };
        myechart.setOption(option);
        this.loading2 = true;
      }
    });
  }

  constructor(private http: HttpClient, private router: Router) {
  }

  ngOnInit(): void {
    this.getBookData();
    this.getUserData();
  }

}
